<template>
	<view>
		<view class="">
			<!-- 2.0.19支持autoBack，默认为false -->
			<u-navbar leftText="登录" :autoBack="true">
			</u-navbar>
		</view>
		<view style="width: 100%;margin-top: 180px;">
			<u-image src="/static/logo/logo.png" width="100%" height="154px" mode="widthFix"></u-image>
		</view>
		<view>

			<u-button :customStyle="btnStyle" @click="wecLogin">微信用户一键登录</u-button>
			<!-- <u-button :customStyle="btnStyle" open-type="getPhoneNumber" @getphonenumber="getphonenumber">微信手机号一键登录</u-button> -->
			<u-button :customStyle="btnStyle1" @click="telLogin">手机号码登录/注册</u-button>
		</view>
		<view style="text-align: center; position: fixed; bottom: 40rpx; width: 100%;">


			<text style="font-size: 24rpx;">登录代表您同意 <text style="color: #0B868E;" @click="tolinkfile">用户服务协议</text></text>
		</view>
		<u-toast ref="uToast" />
	</view>
</template>

<script>
	import {
		weLogin
	} from "@/service/loginService.js";
	import cookie from "@/util/store/cookie.js";
	export default {
		data() {
			return {
				btnStyle: {
					width: '94%',
					marginLeft: '3%',
					height: '100rpx',
					borderRadius: '60rpx',
					backgroundImage: "url('https://resourse.cnlhjt.com/upload/20220825/d1aa35c1d129137d9b12c8f34e59c5bc.png')",
					backgroundSize: '100% 100rpx',
					fontSize: '34rpx',
					color: '#FFFFFF'
				},
				btnStyle1: {
					width: '94%',
					marginLeft: '3%',
					height: '100rpx',
					borderRadius: '60rpx',
					marginTop: '40rpx',
					fontSize: '34rpx',
					backgroundSize: '100% 80rpx',
					color: '#333333'
				},
				code:''
			}
		},
		onLoad() {
			var that=this;
			uni.login({
				provider:'weixin',
				success(res) {
					that.code=res.code
				}
			})
		},
		methods: {
			wecLogin() {
				uni.getUserProfile({
					desc: '获取您的头像，昵称及性别',
					success: function(loginRes) {
						// console.log(loginRes);
						let userInfo = loginRes.userInfo;
						uni.login({
							provider: 'weixin',
							success: function(loginResult) {
								// console.log(loginResult);
								weLogin({
									username: userInfo.nickName,
									avatar: userInfo.avatarUrl,
									sex: userInfo.gender,
									we_code: loginResult.code
								}).then(res => {
									if (res.code == 200) {
										uni.$u.vuex('isLogin', true);
										uni.$u.vuex('uid', res.result.id);
										uni.$u.vuex('username', res.result.username);
										uni.$u.vuex('userid', res.result.userid);
										uni.$u.vuex('avatar', res.result.avatar);
										uni.$u.vuex('sex', res.result.sex);
										uni.$u.vuex('token', res.result.token);
										uni.$u.vuex('phone', res.result.phone);
										uni.$u.vuex('home', res.result.house);
										uni.$u.vuex('userType', res.result.type);
										cookie.set('token', res.result.token);
										cookie.set('uid', res.result.id);
										uni.$u.route({
											url: '/pages/index/index',
											type: 'redirectTo'
										});
									} else {
										uni.$refs.uToast.show({
											type: 'error',
											message: res.msg,
											complete() {
												uni.navigateTo({
													url: '/pages/login/index'
												})
											}
										});
									}
								})
							}
						});

					}
				});
			},
			getphonenumber(e){
				// console.log(e);
				if (!e.detail.iv) {
					uni.showToast({
						title:'获取手机号失败',
						icon:'none'
					})
					return;
				}
				var that = this;
				//验证code值是否过期
				uni.checkSession({
					success(val){
						if(val.errMsg == 'checkSession:ok' ){
							var obj={
								code : that.code,
								iv:e.detail.iv,
								encryptedData :e.detail.encryptedData,
							};
							that.decryptPhone(obj);
						}else{
							uni.login({ 
								provider:'weixin',
								success(res) {
									let code = res.code;
									var obj={
										code,
										iv:e.detail.iv,
										encryptedData:e.detail.encryptedData,
									};
									that.decryptPhone(obj);
								}
							})
						}
					}
				})
			},
			// 调取获取手机号码API
			decryptPhone(obj){
				
			},
			telLogin() {
				this.$u.route({
					url: '/pages/login/tel-login',
					params: {

					}
				})
			},
			tolinkfile(){
				this.$u.route({
					url: '/pages/login/linkfile'
				});
			}
		}
	}
</script>

<style lang="scss">
	page {
		background-color: #FFFFFF;
	}
</style>
